<template>
  <div>
    <div style="display: flex;flex-direction: column">
      <div style="display: flex">
        <div>
          <el-upload action="" :on-change="handleChange" :show-file-list="false"
                     accept="*.mp4" multiple :auto-upload="false">
            <div style="display: flex;align-items: center">
              <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
            </div>
          </el-upload>
        </div>
        <div>
          <el-tooltip content="最大限制50MB，点击文件名可预览" placement="top">
            <i class="el-icon-question" style="color: red"/>
          </el-tooltip>
        </div>
      </div>
      <div :key="$store.state.videoFileList" style="display:flex;flex-direction: row">
        <div v-for="(item,index) in $store.state.videoFileList"
             :key="index" style="display: flex;align-items: flex-start;flex-direction: row">
          <div style="display: flex;flex-direction: column;align-items: center">
            <video :src="item.url" autoplay style="width: 120px;height: 120px;" @click="handlePreview(item)"/>
            <div class="overflow">{{ item.name }}</div>
            <el-button type="danger" round size="mini" @click="handleRemove(index)">删除</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import appUtils, {formatDate, isBlank, isFlag} from '@/utils/util'

export default {
  name: 'VideoUploadList',
  props: ['videoUrl', 'size', 'maxNum'],
  data() {
    return {
      myImage: appUtils.myImage
    }
  },
  watch: {
    '$store.state.videoUploadList': function () {
      // if (this.$store.state.videoFileList.length > 2) {
      //   this.$store.state.videoFileList.splice(0, this.$store.state.videoFileList.length - 1)
      // }
    }
  },
  mounted() {
  },
  methods: {
    isFlag,
    isBlank,
    formatDate,
    handleChange(file, fileList) {
      this.handleMaxSize(file, this.size).then(res => {
        if (isFlag(res, true)) {
          this.$store.state.videoFileList = []
          this.$store.state.videoUploadList = []
          fileList.forEach(value => {
            if (!isBlank(value.uid)) {
              if (this.$store.state.videoFileList.length < this.maxNum) {
                this.$store.state.videoFileList.push({
                  url: URL.createObjectURL(value.raw) + '',
                  name: value.name
                })
                this.$store.state.videoUploadList.push(value.raw)
              }
            }
          })
        } else {
          return this.$message.info('文件过大，请重新选择')
        }
      })
    },
    handleMaxSize(file, size) {
      if (isBlank(size) || isFlag(size, 0)) {
        size = 50
      }
      const fileSize = file.size / 1024 / 1024 < size
      return new Promise((resolve, reject) => {
        if (!fileSize) {
          this.$message.info('文件过大,请不要超过' + size + 'M.')
          return resolve(false)
        } else {
          return resolve(true)
        }
      })
    },
    handleRemove(index) {
      this.$store.state.videoFileList.splice(index, 1)
      this.$store.state.videoUploadList.splice(index, 1)
    },
    handlePreview(item) {
      window.open(item.url)
    }
  }
}
</script>

<style scoped>
.overflow {
  width: 150px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  -o-text-overflow: ellipsis;
}
</style>
